package com.api.smoke.wall;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.api.util.repo.impl.ZyncroRepo;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.ObjectResultApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class UploadAttachInEventSmoke extends AbstractApiTest {

	private String eventUrn;

	@BeforeClass
	protected void setUp() throws Exception {

		final ZyncroRepo zyncroRepo = getRepo();
		final String domainName = this.getClass().getSimpleName();
		final DomainUser mainAdmin = zyncroRepo.getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		this.eventUrn = zyncroRepo.getPropertyDto(domainName, new DtoFactory<String>() {

			public String createDto() {

				try {
					String result = getApi().getService(IWallApiService.class).publishInCompanyFeed("comentario corporativo", null, null,
							null, null, null, false, false);

					return result;

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
			}
		});
	}

	@Test
	public void testUploadAttachedInEvent() throws ZyncroApiException, FileNotFoundException {
		File file = new File("pom.xml");
		String fileName = file.getName();
		Long length = file.length();

		FileInputStream inputStream = new FileInputStream(file);

		String parentDocumentUrn = null;

		ObjectResultApi<String> result = getApi().getService(IWallApiService.class).attachInEvent(eventUrn, getApi().getAccessKey(),
				parentDocumentUrn, fileName, length, inputStream);

		Assert.assertNotNull(result.getObject());
	}
}
